package com.sgb.packers.view.activity;


import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.link.router.PathTeam;
import com.sgb.packers.R;
import com.sgb.packers.databinding.PackersActivityAddProjectBinding;
import com.sgb.packers.entity.ProjectEntity;
import com.sgb.packers.view.fragment.GetJobFragment;
import com.sgb.packers.viewmodel.AddProjectViewModel;
import com.swgk.core.common.base.TeamBaseActivity;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.interfaces.DialogClick;
import com.swgk.core.util.MToast;
import com.swgk.core.util.MapUtils;
import com.swgk.core.util.NewDialogHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author younghow
 * @date 2021/11/12
 * description:个人记工主页
 */
@Route(path = PathTeam.AddProjectActivity)
public class AddProjectActivity extends TeamBaseActivity<AddProjectViewModel, PackersActivityAddProjectBinding> {

    private List<ProjectEntity> list;
    public MutableLiveData<String> nameLiveData = new MutableLiveData<>();
    public MutableLiveData<String> newLiveData = new MutableLiveData<>();

    @Override
    protected Class<AddProjectViewModel> createVM() {
        return AddProjectViewModel.class;
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        mBinding.actionBarTab.setOnBackClickListener(v -> finish());
        mBinding.actionBarTab.setSelectTab((position, s) -> {
            if (position > 0) {
                MToast.showToast(AddProjectActivity.this, "功能开发中");
            }
        });
        list = new ArrayList<>();
        initLiveData();
        initListener();
    }

    private void initListener() {
        mBinding.tvNewProject.setOnClickListener(this);
    }

    private void initLiveData() {
        mViewModel.mGetProjectLiveData.observe(this, projectEntities -> {
            list = projectEntities;
            if (list != null && list.size() > 0) {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.add(R.id.rl_fragment, GetJobFragment.newInstance(list));
                fragmentTransaction.commit();
                mBinding.llFragment.setVisibility(View.GONE);
                mBinding.rlFragment.setVisibility(View.VISIBLE);
            }else {
                mBinding.llFragment.setVisibility(View.VISIBLE);
                mBinding.rlFragment.setVisibility(View.GONE);
            }
        });
        mViewModel.mAddProjectLiveData.observe(this, aBoolean -> mViewModel.getProject(new MapUtils().pair2Map(Pair.create("type", 1))));
    }

    @Override
    protected void initData() {
        mBinding.actionBarTab.setDataForStr("记工", "记账");
        mViewModel.getProject(new MapUtils().pair2Map(Pair.create("type", 1)));
    }

    @Override
    protected int layoutId() {
        return R.layout.packers_activity_add_project;
    }

    @Override
    protected void onMultiClick(View v) {
        super.onMultiClick(v);
        if (v.getId() == R.id.tv_new_project) {
            RoundCornerDialog newDialogHelper = new NewDialogHelper<String>(this)
                    .editDialog(false, true, "新建项目", "请输入项目名称", "确定", "取消")
                    .setDialogClick(new DialogClick<String>() {
                        @Override
                        public void confirmClick(String s, Dialog dialog) {
                            boolean haveProject = false;
                            if (list != null && list.size() > 0) {
                                for (ProjectEntity projectEntity : list) {
                                    if (projectEntity.getProjectName().equals(s)) {
                                        haveProject = true;
                                        break;
                                    }
                                }
                            }
                            if (haveProject) {
                                MToast.showToast(AddProjectActivity.this, "项目名称重复，请重新输入");
                            } else {
                                mViewModel.addProject(new MapUtils().pair2Map(Pair.create("projectName", s), Pair.create("type", 1)));
                                dialog.cancel();
                            }
                        }
                    })
                    .build();
            newDialogHelper.show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && resultCode == 2){
            if (!TextUtils.isEmpty(data.getStringExtra("name"))) {
                String name = data.getStringExtra("name");
                if (name.contains("---")){
                    String newName = name.replace("---","");
                    newLiveData.postValue(newName);
                } if ("没有项目".equals(data.getStringExtra("name"))){
                    mViewModel.getProject(new MapUtils().pair2Map(Pair.create("type", 1)));
                }else if (!name.contains("---")){
                    nameLiveData.postValue(data.getStringExtra("name"));
                }
            }
        }
    }
}